package com.tang.managementdemo.controller;

import com.tang.managementdemo.pojo.Dept;
import com.tang.managementdemo.pojo.Result;
import com.tang.managementdemo.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     */
    @GetMapping
    public Result list(){
        List<Dept> deptList = deptService.findAll();
        log.info("查询到部门列表：{}",deptList);
        return Result.success(deptList);
    }

    //删除部门信息
    @DeleteMapping
    public Result delete(Integer id){
        deptService.delete(id);
        log.info("删除部门：{}",id);
        return Result.success();
    }

    //添加部门信息
    @PostMapping
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }

    //根据Id获取部门信息
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    //修改部门信息
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }
}